package cn.iocoder.yudao.module.security.service.warnlog;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.security.controller.admin.warnlog.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.warnlog.SecurityWarnLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.security.dal.mysql.warnlog.SecurityWarnLogMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.security.enums.ErrorCodeConstants.*;

/**
 * 告警日志 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class SecurityWarnLogServiceImpl implements SecurityWarnLogService {

    @Resource
    private SecurityWarnLogMapper warnLogMapper;

    @Override
    public Long createWarnLog(SecurityWarnLogSaveReqVO createReqVO) {
        // 插入
        SecurityWarnLogDO warnLog = BeanUtils.toBean(createReqVO, SecurityWarnLogDO.class);
        warnLogMapper.insert(warnLog);
        // 返回
        return warnLog.getId();
    }

    @Override
    public void updateWarnLog(SecurityWarnLogSaveReqVO updateReqVO) {
        // 校验存在
        validateWarnLogExists(updateReqVO.getId());
        // 更新
        SecurityWarnLogDO updateObj = BeanUtils.toBean(updateReqVO, SecurityWarnLogDO.class);
        warnLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarnLog(Long id) {
        // 校验存在
        validateWarnLogExists(id);
        // 删除
        warnLogMapper.deleteById(id);
    }

    private void validateWarnLogExists(Long id) {
        if (warnLogMapper.selectById(id) == null) {
            throw exception(WARN_LOG_NOT_EXISTS);
        }
    }

    @Override
    public SecurityWarnLogDO getWarnLog(Long id) {
        return warnLogMapper.selectById(id);
    }

    @Override
    public PageResult<SecurityWarnLogDO> getWarnLogPage(SecurityWarnLogPageReqVO pageReqVO) {
        return warnLogMapper.selectPage(pageReqVO);
    }

}